package com.bs.resa.web;

import com.bs.resa.common.RedisUtils;
import com.bs.resa.dao.AdminUserMapper;
import com.bs.resa.pojo.AdminUser;
import com.bs.resa.pojo.Module;
import com.bs.resa.pojo.PType;
import com.bs.resa.pojo.Project;
import com.bs.resa.service.*;
import com.sun.org.apache.xpath.internal.operations.Mod;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lin
 * @date 2019/3/23 20:38
 */
@Controller
public class ProjectViewController {
    private static Logger logger= LoggerFactory.getLogger(ProjectController.class);
    protected Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
    @Resource
    RedisUtils redisUtils;

    @Autowired
    ProjectTypeService projectTypeService;
    @Autowired
    ModuleService moduleService;
    @Autowired
    ProjectService projectService;
    @Autowired
    TaskService taskService;
    @Autowired
    AdminUserService adminUserService;

    @ApiOperation(value = "跳转到我的任务",notes = "GET")
    @RequestMapping(value = "task/mytask.do",method = RequestMethod.GET)
    @RequiresPermissions(value = "/resa/task/mytask.do")
    public ModelAndView mytask(ModelAndView model){
        model.setViewName("task/task_charge");
        return model;
    }
    @ApiOperation(value = "跳转到完成任务",notes = "GET")
    @RequestMapping(value = "task/task_table.do",method = RequestMethod.GET)
    public ModelAndView successTask(ModelAndView model){
        model.setViewName("task/task_table");
        return model;
    }
    @ApiOperation(value = "跳转到创建项目页面",notes = "GET")
    @RequestMapping(value = "project/project_create.do",method = RequestMethod.GET)
    @RequiresPermissions("/resa/project/project_create.do")
    public ModelAndView projectCreate(ModelAndView model){
        List<PType> pTypes=projectTypeService.getProjectTypeList();
//        logger.info("项目类型："+pTypes);
        List<Module>  modulesList=moduleService.getModuleList();
//        logger.info("项目组件："+modulesList);
        List<AdminUser> admin=adminUserService.queryAll();
        model.addObject("ptype",pTypes);
        model.addObject("module",modulesList);
        model.addObject("admin",admin);
        model.setViewName("project/project_create");
        return model;
    }

    @ApiOperation(value = "跳转到项目列表页面",notes = "GET")
    @RequestMapping(value = "project/projects.do",method = RequestMethod.GET)
    @RequiresPermissions("/resa/project/projects.do")
    public ModelAndView projectsList(@RequestParam(value = "projectName" ,required = false)String projectName, ModelAndView model){
//        logger.info("跳转到项目库页面");
        System.out.println(projectName);
//        logger.info("查询所有项目");
        List<Map<String,Object>> projectList=projectService.queryAll(projectName);
//        System.out.println(projectList);
//        logger.info("项目数："+projectList.size());
        model.addObject("projectList",projectList);
        model.setViewName("project/projects");
        return model;
    }

    @ApiOperation(value = "跳转到项目详情页面",notes = "GET")
    @RequestMapping(value = "project/project_detail/{pid}.do",method = RequestMethod.GET)
    @RequiresPermissions(value = "project/project_detail")
    public ModelAndView projectsdetail(ModelAndView model, @PathVariable("pid")String pid) throws ParseException {
//        logger.info("项目id:"+pid);
//        logger.info("跳转到项目详情页面");
        List<Module> moduleList=moduleService.queryModuleByPid(pid);
        //图表上的数据
        Map<String,Object> data=projectService.getDetailOther(pid);
        System.out.println("图表数："+data);
        model.addObject("data",data);
        model.addObject("modulList",moduleList);
        model.addObject("pid",pid);
        redisUtils.set("pid",pid);
//        redisUtils.set("pid",pid);
        model.setViewName("project/project_detail");
        return model;
    }


//优化，redis

    @ApiOperation(value = "项目详情-甘特时间视图",notes = "GET")
    @RequestMapping(value = "project/gantetu/gantetu/{pid}.do",method = RequestMethod.GET)
    public ModelAndView projectsGantetu(ModelAndView model, @PathVariable("pid")String pid){
        logger.info("项目id:"+pid);
        logger.info("甘特时间视图");
//        taskService.queryTaskByPid(pid);
        model.addObject("pid",pid);
        model.setViewName("project/gantetu/gantetu");
        return model;
    }

    @ApiOperation(value = "项目详情-报表视图",notes = "GET")
    @RequestMapping(value = "project/baobiao/plain_page/{pid}.do",method = RequestMethod.GET)
    public ModelAndView projectsbaobiao(ModelAndView model, @PathVariable("pid")String pid){
        logger.info("项目id:"+pid);
        logger.info("报表视图");

        List<Map<String,Object>> projectTypeList=projectTypeService.queryProjectTypeByPid(pid);
        List<Map<String,Object>> projectStatusList=projectTypeService.queryProjectStatus(pid);
        List<Map<String,Object>> projectCountList=projectTypeService.queryCount(pid);
        System.out.println("报表数据-总"+projectCountList);
        model.addObject("pid",pid);
        model.addObject("projectTypeList",projectTypeList);
        model.addObject("projectStatusList",projectStatusList);
        model.addObject("projectCountList",projectCountList);
        model.setViewName("project/baobiao/plain_page");
        return model;
    }

    @ApiOperation(value = "项目设置-人员配置视图",notes = "GET")
    @RequestMapping(value = "bumen/index.do",method = RequestMethod.GET)
    public ModelAndView projectsbaobiao(ModelAndView model) {
        model.setViewName("bumen/index");
        return model;
    }
    @ApiOperation(value = "项目详情-设置视图",notes = "GET")
    @RequestMapping(value = "project/setting/setting_page/{pid}.do",method = RequestMethod.GET)
    public ModelAndView projectssetting(ModelAndView model, @PathVariable("pid")String pid){
//        logger.info("项目id:"+pid);
//        logger.info("跳转到项目详情设置视图页面");

        Map<String,Object> project=projectService.queryByPid(pid);
        System.out.println("项目数据");
        System.out.println(project.toString());
        List<AdminUser> admin=adminUserService.queryAll();
//            List<AdminUser> admin=adminUserService.queryByPid(pid);
        model.addObject("admin",admin);
        model.addObject("pid",pid);
        model.addObject("data",project);
        model.setViewName("project/setting/setting_page");
        return model;
    }


}
